<template>
  <div class="regist">
    <div class="box">
      <h2>在线教育管理系统</h2>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
          <el-input v-model="ruleForm.password2"></el-input>
        </el-form-item>
        <el-form-item label="身份" prop="identity">
          <el-select v-model="ruleForm.identity" placeholder="请选择身份">
            <el-option label="管理员" value="管理员"></el-option>
            <el-option label="普通员工" value="普通员工"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { enroll } from "../utils/home";
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: "米斯特吴",
        email: "",
        password: "321321",
        password2: "321321",
        identity: "管理员",
      },
      rules: {
        name: [{ required: true, message: "请填写用户名", trigger: "blur" }],
        email: [{ required: true, message: "请填写邮箱", trigger: "blur" }],
        password2: [{ validator: validatePass2, trigger: "blur" }],
        identity: [
          { required: true, message: "请选择身份", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 注册成功

          enroll(this.ruleForm).then(res => {
            // console.log(123);
            console.log(res);
            if (res._id) {
             this.$router.push('/login')
            } else {
              alert("错误");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.regist {
  color: #fff;
  display: flex;
  .box {
    width: 700px;
    margin: 180px 500px;
    h2 {
      text-align: center;
      margin-bottom: 20px;
    }
  }
}
::v-deep .el-form-item__label {
  color: #fff !important;
}
::v-deep .el-button--primary {
  width: 100%;
}
</style>